/*
 * @Description:
 *
 * @Date: 2021-03-01 17:50:54
 */
import Vue from 'vue'
import App from './App.vue'
import mobileErr from '@/views/error/mobileErr'
import store from './store'
import router from './router'
import './router/permission.js'
import power from './config/config.power'
// 引入服务
import API from './api'
import Axios from './utils/axios'
import Tools from './utils/utils'
import { handleArryToTree } from '@/utils/utils'
// 引入全局组件
import '@/components/index'
// 引入全局指令
import '@/directives/index'
// 引入全局mixin
import globalMixin from '@/mixin/index'
import calc from '@/utils/numCalc.js'
import config from '@/config/config.base'
import globalDict from '@/config/config.dict.js'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import $ from 'jquery'
Vue.prototype.$ = $
import 'bootstrap/dist/js/bootstrap.min.js'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'popper.js/dist/umd/popper.js'
import 'bootstrap/dist/css/bootstrap.min.css'
import '@/assets/css/helper.css'
import '@/assets/css/menu.scss'
import modal from '@/utils/modal'

Vue.mixin(globalMixin)
Vue.use(API)
Vue.use(Axios)
Vue.use(ElementUI)
// Vue.use(VueAwesomeSwiper)

Vue.config.productionTip = true
Vue.config.devtools = true
Vue.prototype.$config = config
Vue.prototype.$calc = calc
Vue.prototype.$modal = modal
Vue.prototype.$globalDict = globalDict
Vue.prototype.handleTree = handleTree

// 挂载权限标识
Vue.prototype.$btn = power
// 获取菜单权限
let urlData = Tools.parseURL(window.location.href)
console.log('url参数', urlData)

function init() {
	new Vue({
		router,
		store,
		render: (h) => h(App),
	}).$mount('#app')
}
function initWechat() {
	new Vue({
		router,
		render: (h) => h(mobileErr),
	}).$mount('#app')
}

// 获取分类列表
function getArticleTypeList() {
	// init()
	// return
	const list = []
	const typeListAll = API.articletype.getTypeListAll()
	list.push(typeListAll)
	const articleType = API.articletype.getArticletype()
	list.push(articleType)
	Promise.all(list).then(results => {
		// 所有文章菜单列表
		const typeListAll = results[0]
		const typeData = typeListAll.data
		// 可选择菜单列表
		const menus = results[1]
		const data = menus.data

		if (data.code == 200 && typeData.code == 200) {
			const rows = data.rows
			const tree = handleArryToTree(rows, -1)
			const allTree = handleArryToTree(typeData.rows, -1)
			console.log('tree', allTree)
			store.commit('menu/setMenus', tree)
			store.commit('menu/setAllMenus', allTree)
			store.commit('menu/setLevelMenus', typeData.rows)
			store.commit('appData/setArticleType', typeData.rows)
			addResize()
			init()
		}
	})
}

function handleResize() {
	store.commit('appConstant/setScreenWidth', window.innerWidth)
}

function addResize() {
	store.commit('appConstant/setScreenWidth', window.innerWidth)
	window.addEventListener('resize', handleResize, true)
}

// 获取菜单列表
getArticleTypeList()


/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data, id, parentId, children) {
	let config = {
		id: id || 'id',
		parentId: parentId || 'parentId',
		childrenList: children || 'children'
	};

	var childrenListMap = {};
	var nodeIds = {};
	var tree = [];

	for (let d of data) {
		let parentId = d[config.parentId];
		if (childrenListMap[parentId] == null) {
			childrenListMap[parentId] = [];
		}
		nodeIds[d[config.id]] = d;
		childrenListMap[parentId].push(d);
	}

	for (let d of data) {
		let parentId = d[config.parentId];
		if (nodeIds[parentId] == null) {
			tree.push(d);
		}
	}

	for (let t of tree) {
		adaptToChildrenList(t);
	}

	function adaptToChildrenList(o) {
		if (childrenListMap[o[config.id]] !== null) {
			o[config.childrenList] = childrenListMap[o[config.id]];
		}
		if (o[config.childrenList]) {
			for (let c of o[config.childrenList]) {
				adaptToChildrenList(c);
			}
		}
	}
	return tree;
}
